/*
 * @lc app=leetcode.cn id=605 lang=cpp
 *
 * [605] 种花问题
 */
#include "include.h"
// @lc code=start
class Solution {
public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) {
        flowerbed.push_back(0);

        int size = flowerbed.size();
        // printf("start %d\n", size);
        int volumn = 0;
        int emptyGround = 1;

        for (int i=0;i<size;i++){
            if (flowerbed[i] == 0){
                emptyGround++;
                // printf("check it %d %d\n", emptyGround, volumn);
            }else{
                volumn += plantFlowers(emptyGround);
                emptyGround = 0;
                // printf("check it %d %d\n", emptyGround, volumn);
            }
        }
        volumn += plantFlowers(emptyGround);
        return volumn >= n;
    }
    int plantFlowers(int emptyGround){
        if (emptyGround == 0){return 0;}
        if (emptyGround%2==1){return emptyGround/2;}
        else {return (emptyGround-1)/2;}
    }
};
// Accepted
// 124/124 cases passed (8 ms)
// Your runtime beats 97.52 % of cpp submissions
// Your memory usage beats 98.52 % of cpp submissions (19.6 MB)

// int main(){
//     vector<int> myTest{1,0,0,0,1,0,0};
//     Solution mySolution;
//     mySolution.canPlaceFlowers(myTest, 2);
//     return 0;
// }
// @lc code=end

